package com.liusha.quicksand.web.admin;

import com.liusha.quicksand.service.BlogService;
import com.liusha.quicksand.vo.BlogQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: 一世长安一世欢
 * @Date: 2020/10/19
 * @Time: 23:26
 * @Description: No Description
 */

@Controller
@RequestMapping("/admin")
public class BlogController {

    private static final String REDIRECT_LIST="redirect:/admin/article-management";

    @Autowired
    private BlogService blogService;
//
//    @Autowired
//    private TypeService typeService;

    @GetMapping("/article-management")
    public String blogs(@PageableDefault(size = 12,sort = {"updatetime"},direction = Sort.Direction.DESC) Pageable pageable, BlogQuery blog, Model model) {

        model.addAttribute("page",blogService.listBlog(pageable,blog));
        return "admin/article-management";
    }

    @PostMapping("/article-management/search")
    public String search(@PageableDefault(size = 12,sort = {"updatetime"},direction = Sort.Direction.DESC) Pageable pageable, BlogQuery blog, Model model) {

        model.addAttribute("page",blogService.listBlog(pageable,blog));
        return "admin/article-management :: blogList";
    }


//    /*后台管理   对文章做编辑操作后点击调用*/
//    @PostMapping("/{list}/{article-management}")
//    public String post(Blog blog, RedirectAttributes attributes, HttpSession session) {
//
//        blog.setUser((User) session.getAttribute("user"));
//
//        Blog b = blogService.saveBlog(blog);
//
//        if (b == null) {
//            attributes.addFlashAttribute("message", "修改失败");
//        } else {
//            attributes.addFlashAttribute("message", "修改成功");
//        }
//
//        return REDIRECT_LIST;
//
//    }


    /*后台管理员对文章做删除操作*/
    @GetMapping("/article-management/{blogId}/delete")
    public String delete(@PathVariable Long blogId, RedirectAttributes attributes){

        blogService.deleteBlog(blogId);

        attributes.addFlashAttribute("message","删除成功！");

        return REDIRECT_LIST;
    }

//    @GetMapping
//    public String ppost(){
//
//    }

//    /*后台管理员修改操作*/
//    @GetMapping("/{list}/{article-management}/{blogId}/input")
//    public String editInput(@PathVariable Long blogId, Model model) {
//
//        Blog blog = blogService.getBlog(blogId);
//
////        blog.init();
//
//        model.addAttribute("blog", blog);
//
//        return "admin/p-post";
//    }
}
